/**
 * 
 */
package vanhoc.android.mediakool;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vanhoc.android.mediakool.R;
import vanhoc.android.mediakool.custom.adapter.ArtistGridAdapter;
import vanhoc.android.mediakool.db.DatabaseHelper;
import vanhoc.android.medialcool.item.data.ArtistItemData;
import vanhoc.android.medialcool.utils.ConnectionDetector;
import vanhoc.android.medialcool.utils.FileUtils;
import vanhoc.android.medialcool.utils.HttpUtils;
import vanhoc.android.medialcool.utils.ImageHelper;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

/**
 * @author Administrator
 *
 */

//Neu chua co thi :

	//Tao thu muc /My music/thumb/

	//kiem tra xem co ket noi internet khong

	//Search Google de lay ve link hinh cua cac artist theo ten cua artist

	//sau do thu muc sdcard/My music/thumb/...
	
	//tai cac file hinh vao thu muc thumb/
	
	//Neu co thu muc My music/thumb/ roi thi duyet qua danh sach ten artist roi so sanh,kiem tra trong thu muc do co hinh(ten hinh) tuong ung voi ten cua artist
		//(Luu y : ten hinh dat theo artist name)
	//Neu artist nao thieu ten thi chi tai hinh cua artist do thoi
	
	//Neu gia su co 10 artist ma trong thu muc co 10 thumb tuong ung roi thi thoi 


public class ArtistsActivity extends Activity implements OnItemClickListener {
	GridView gridArtist;
	
	//id bai hat dang hat, mac dinh chua co bai hat nao hat thi gan  = 0
	int songPlayingID = 0;
		
	DatabaseHelper databaseHelper;
	
	private final static String FIELDNAME = "artist";
	
	ArrayList<ArtistItemData> artistItemList;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.artist);
		
		databaseHelper = new DatabaseHelper(this);
		//Khi dang choi nhac o min hinh MusicPlayerActivity, user back lai man hinh TabControlActivity se gui qua Intent id cua bai hat dang choi
		//Con khi moi mo ung dung len(tuc la chua choi nha) thi khong co Intent
		//lay ve id cua bai hat dang choi quan intent (neu co) -> them icon cho biet la co 1 bai hat dang hat trong album nao
		this.songPlayingID	=	 getIntent().getExtras().getInt("songPlayingID");
		
		//Lay ve ds cac artist item (ten + thumb path) tu db
		artistItemList = getArtistItemList();
		
		initArtistView();
		//new getImagesTask().execute();
	}

	/**
	 * init grid view
	 */
	private void initArtistView() {
		gridArtist	=	(GridView) findViewById(R.id.gridArtist);
		
		//adapter
		ArtistGridAdapter adapter = new ArtistGridAdapter(getApplicationContext(), R.id.gridArtist, artistItemList);
		gridArtist.setAdapter(adapter);
		
		gridArtist.setOnItemClickListener(this);
	}
	
	
	
	private ArrayList<ArtistItemData> getArtistItemList() {
		ArrayList<ArtistItemData> itemList = new ArrayList<ArtistItemData>();
		databaseHelper.openToRead();
		Cursor cursor	=	databaseHelper.getArtistItemList();
		cursor.moveToFirst();
		for (int i = 0; i < cursor.getCount(); i++) {
			ArtistItemData item = new ArtistItemData();
			
			item.setName(cursor.getString(0));
			item.setThumbPath(cursor.getString(1));
			
			itemList.add(item);
			cursor.moveToNext();
		}
		databaseHelper.close();
		
		return itemList;
	}
	
	
	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		Intent it = new Intent(getApplicationContext(), SongsActivity.class);
		
		//put key : tuc la ten field trogn song table (de biet dc lay ds bai hat cua 1 genre, hay 1 album, hay 1 playlist nao)
		it.putExtra("FIELDNAME", FIELDNAME);
		
		//put value : tuc la ten field can lay ( vidu lay ds bai hat cua Genre co title la POP)
		it.putExtra("FIELDVALUE", artistItemList.get(position).getName());
		
		//Put id cua bai dang hat sang (neu khong co id = 0)
		it.putExtra("songPlayingID", ArtistsActivity.this.songPlayingID);
				
		startActivity(it);
	}
	
	@Override
	public void onBackPressed() {
		Intent it = new Intent(getApplicationContext(), DashboardActivity.class);
    	startActivity(it);
	}
}
